#include "mqtt_client.h"

#include <MQTTClient.h>
#include <string.h>
#include <unistd.h>

#include "cJSON.h"
#include "common.h"

#include "log.h"

int main(void) {
	MQTTClient client = mqtt_client();
	do {
		sleep(1);
	} while (!client);
	if (MQTTClient_subscribe(client, "服务器/#",
							 MQTTREASONCODE_GRANTED_QOS_1) !=
		MQTTCLIENT_SUCCESS) {
		log_warn("MQTT客户端订阅失败");
	}
	cJSON *json = cJSON_CreateObject();
	uint32_t device = 0;

	while (1) {
		// log_trace("主函数 死循环");
		cJSON_AddNumberToObject(json, "时间", common_get_timestamp());
		mqtt_publish("网关/时间", json);
		cJSON_DeleteItemFromObject(json, "时间");
		sleep(2);

		cJSON_AddNumberToObject(json, "设备上传数据", device++);
		mqtt_publish("设备/上传", json);
		cJSON_DeleteItemFromObject(json, "设备上传数据");
		sleep(1);
	}
	return 0;
}